package su.myspringapps.online_school_learning_spring_data_jpa.repository;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import su.myspringapps.online_school_learning_spring_data_jpa.entity.Account;
import su.myspringapps.online_school_learning_spring_data_jpa.repository.second_db.AccountRepository;

import java.util.List;

@SpringBootTest
class AccountRepositoryTest {

    @Autowired
    AccountRepository accountRepository;

    @Test
    void findAll() {
        List<Account> accountList = accountRepository.findAll();
        Account actualAccount1 = accountList.get(0);
        Account actualAccount2 = accountList.get(1);
        Account actualAccount3 = accountList.get(2);

        Assertions.assertEquals(1, actualAccount1.getId());
        Assertions.assertEquals("navotno_stoechko", actualAccount1.getLogin());
        Assertions.assertEquals("password1", actualAccount1.getPassword());

        Assertions.assertEquals(2, actualAccount2.getId());
        Assertions.assertEquals("semar_zdrahar", actualAccount2.getLogin());
        Assertions.assertEquals("password2", actualAccount2.getPassword());

        Assertions.assertEquals(3, actualAccount3.getId());
        Assertions.assertEquals("sedayko_stumchik", actualAccount3.getLogin());
        Assertions.assertEquals("password3", actualAccount3.getPassword());
    }

    @Test
    void findById() {
        Account actualAccount = accountRepository.findById(1);
        Assertions.assertEquals(1, actualAccount.getId());
        Assertions.assertEquals("navotno_stoechko", actualAccount.getLogin());
        Assertions.assertEquals("password1", actualAccount.getPassword());
    }

    @Test
    void save() {
        saveAndDeleteByIdFunctional();
    }

    @Test
    void update() {
        Account newAccount = new Account();
        newAccount.setLogin("shantor_cherviz");
        newAccount.setPassword("password4");
        Integer id = accountRepository.save(newAccount).getId();
        Account currentAccount = accountRepository.findById(id);
        Assertions.assertEquals(newAccount, currentAccount);
        String newLogin = "tanya_chumovoz";
        String newPassword = "password5";
        currentAccount.setLogin(newLogin);
        currentAccount.setPassword(newPassword);
        accountRepository.save(currentAccount);
        Account actualAccount = accountRepository.findById(id);
        Assertions.assertEquals(newLogin, actualAccount.getLogin());
        Assertions.assertEquals(newPassword, actualAccount.getPassword());
        accountRepository.deleteById(id);
        currentAccount = accountRepository.findById(id);
        Assertions.assertNull(currentAccount);
    }

    @Test
    void deleteById() {
        saveAndDeleteByIdFunctional();
    }

    @Test
    void delete() {
        Account newAccount = new Account();
        newAccount.setLogin("shantor_cherviz");
        newAccount.setPassword("password4");
        Integer id = accountRepository.save(newAccount).getId();
        Account currentAccount = accountRepository.findById(id);
        Assertions.assertEquals(newAccount, currentAccount);
        accountRepository.delete(currentAccount);
        currentAccount = accountRepository.findById(id);
        Assertions.assertNull(currentAccount);
    }

    void saveAndDeleteByIdFunctional() {
        Account newAccount = new Account();
        newAccount.setLogin("shantor_cherviz");
        newAccount.setPassword("password4");
        Integer id = accountRepository.save(newAccount).getId();
        Account currentAccount = accountRepository.findById(id);
        Assertions.assertEquals(newAccount, currentAccount);
        accountRepository.deleteById(id);
        currentAccount = accountRepository.findById(id);
        Assertions.assertNull(currentAccount);
    }
}